<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>健康报备系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!--    <link rel="stylesheet" href="./css/BaoBei.css">-->
    <style>
        /* 现代风格样式 */
        body {
            font-family: 'Segoe UI', system-ui, sans-serif;
            background: #f5f7fa;
            margin: 0;
            padding: 2rem;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 2rem;
            font-weight: 600;
        }

        /* 卡片容器 */
        .card-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
            gap: 1.5rem;
            padding: 1rem;
        }

        /* 单个卡片样式 */
        .member-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .member-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
        }

        .member-info {
            color: #4a5568;
            margin-bottom: 1rem;
        }

        .member-info h3 {
            color: #2c3e50;
            margin: 0 0 0.5rem;
            font-size: 1.2rem;
        }

        .member-info p {
            margin: 0.3rem 0;
            font-size: 0.9rem;
        }

        .report-btn {
            width: 100%;
            padding: 0.8rem;
            background: #4299e1;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background 0.2s;
        }

        .report-btn:hover {
            background: #3182ce;
        }

        /* 报备表单样式 */
        .report-form {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
            z-index: 100;
            width: 90%;
            max-width: 400px;
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #4a5568;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 0.8rem;
            border: 2px solid #e2e8f0;
            border-radius: 8px;
            font-size: 1rem;
        }

        .form-actions {
            display: flex;
            gap: 1rem;
            margin-top: 2rem;
        }

        .form-actions button {
            flex: 1;
            padding: 0.8rem;
            border: none;
            border-radius: 8px;
            cursor: pointer;
        }

        .submit-btn {
            background: #48bb78;
            color: white;
        }

        .cancel-btn {
            background: #f56565;
            color: white;
        }

        /* 遮罩层 */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 99;
        }

        /* 提示信息 */
        .toast {
            position: fixed;
            bottom: 2rem;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(0, 0, 0, 0.9);
            color: white;
            padding: 1rem 2rem;
            border-radius: 8px;
            animation: slideIn 0.3s ease-out;
        }

        @keyframes slideIn {
            from {
                transform: translate(-50%, 100%);
                opacity: 0;
            }

            to {
                transform: translate(-50%, 0);
                opacity: 1;
            }
        }
    </style>
</head>

<body>
<div class="container">
    <h1>家庭成员健康报备</h1>

    <!-- 家庭成员卡片容器 -->
    <div class="card-container" id="cardContainer"></div>

    <!-- 报备表单 -->
    <div class="report-form" id="reportForm" style="display: none;">
        <div class="form-group">
            <label>身份证号</label>
            <input type="text" id="inhabitantId" disabled>
        </div>
        <div class="form-group">
            <label>体温（℃）</label>
            <input type="number" id="temperature" step="0.1" placeholder="36.5">
        </div>
        <div class="form-group">
            <label>心率（次/分钟）</label>
            <input type="number" id="frequency" placeholder="75">
        </div>
        <div class="form-actions">
            <button type="button" class="cancel-btn" onclick="closeForm()">取消</button>
            <button type="submit" class="submit-btn" onclick="submitReport()">提交报备</button>
        </div>
    </div>

    <!-- 遮罩层 -->
    <div class="overlay" id="overlay" style="display: none;"></div>
</div>

<script>
    $(document).ready(function () {
        // 获取家庭成员数据
        $.ajax({
            url: 'http://localhost:8000/user/inhabitant/get',
            headers: {
                'Authorization': getCookie('token')
            },
            success: function (response) {
                renderCards(response.data);
            },
            error: () => {
                document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
                window.location.href = 'http://localhost:5173';
                showToast('获取数据失败', 'error');}
        });

        // 表单提交处理
        $('#reportForm').on('submit', function (e) {
            e.preventDefault();
            submitReport();
        });
    });

    // 渲染卡片
    function renderCards(members) {
        const container = $('#cardContainer');
        members.forEach(member => {
            const card = `
                    <div class="member-card">
                        <div class="member-info">
                            <h3>${member.name}</h3>
                            <p><i class="fas fa-id-card"></i> ${member.id}</p>
                            <p><i class="fas fa-birthday-cake"></i> ${new Date(member.birthday).toLocaleDateString()}</p>
                            <p><i class="fas fa-${member.gender === '男' ? 'mars' : 'venus'}"></i> ${member.gender}</p>
                        </div>
                        <button class="report-btn" onclick="openForm('${member.id}')">
                            <i class="fas fa-plus-circle"></i> 健康报备
                        </button>
                    </div>
                `;
            container.append(card);
        });
    }

    // 打开报备表单
    function openForm(inhabitantId) {
        $('#inhabitantId').val(inhabitantId);
        $('#reportForm, #overlay').fadeIn(200);
    }

    // 关闭表单
    function closeForm() {
        $('#reportForm, #overlay').fadeOut(200);
        $('#temperature, #frequency').val('');
    }

    // 提交报备
    function submitReport() {
        const data = {
            inhabitantId: $('#inhabitantId').val(),
            temperature: $('#temperature').val(),
            frequency: $('#frequency').val()
        };

        $.ajax({
            url: 'http://localhost:8000/user/inhabitant/report/create',
            method: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(data),
            headers: {
                'Authorization': getCookie('token')
            },
            success: function (response) {
                showToast(response.code === 200 ? '报备成功！' : '报备失败');
                closeForm();
            },
            error: () => {
                showToast('请求失败', 'error');
                document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
                window.location.href = 'http://localhost:5173';}
        });
    }

    // 从 cookie 中获取指定名称的值
    function getCookie(name) {
        const value = `; ${document.cookie}`;
        const parts = value.split(`; ${name}=`);
        console.log(parts);
        if (parts.length === 2) return parts.pop().split(';').shift();
    }

    function showToast(message, type = 'info') {
        const toast = $(`<div class="toast">${message}</div>`);
        $('body').append(toast);
        setTimeout(() => toast.remove(), 3000);
    }
</script>
</body>

</html>
